public class EntitlementsConfigController {

    static final String strDefaultIdValue='00N999999999999';

    public PageReference saveSettings() {
        String strUserIdEntry = (''+this.es.Case_Entitlements_Field_Id__c).trim();
        //if (strUserIdEntry.length() == 15) {
            update this.es;
        //}
        return null;
    }
    public EntitlementsConfigController() {
        try {
            this.es = [select Id,Case_Entitlements_Field_Id__c,
                TempDisableCaseTriggers__c from Entitlements_Settings__c Limit 1];
        } catch(Exception e) {
            this.es = new Entitlements_Settings__c(
                Case_Entitlements_Field_Id__c=strDefaultIdValue,
                TempDisableCaseTriggers__c='');
        }
    }
    public Entitlements_Settings__c es { get; set; }
    
    public PageReference init() {
        if (this.es.Id==null) { insert es; } 
        return null;
    }

    static testMethod void testEntitlementsConfigController() {
        // Clear old settings if any
        try {
            Entitlements_Settings__c esArr = [Select Id from Entitlements_Settings__c];
            delete esArr;
        } catch(Exception e) {}
        
        // Do sequence with no preexisting settings
        EntitlementsConfigController gsc = new EntitlementsConfigController();
        gsc.init();
        gsc.es.Case_Entitlements_Field_Id__c = '00N111222333444';
        gsc.es.TempDisableCaseTriggers__c = 'yEs';
        gsc.saveSettings();
        System.assertEquals('yes',[Select TempDisableCaseTriggers__c from Entitlements_Settings__c].TempDisableCaseTriggers__c.toLowerCase());

        // Once more with settings already extant
        EntitlementsConfigController gsc2 = new EntitlementsConfigController();
        gsc2.init();
        gsc2.es.Case_Entitlements_Field_Id__c = '00N111222333444';
        gsc2.es.TempDisableCaseTriggers__c = '';
        gsc2.saveSettings();
        System.assertEquals(1,[Select Count() from Entitlements_Settings__c]);
        System.assertEquals(null,[Select TempDisableCaseTriggers__c from Entitlements_Settings__c].TempDisableCaseTriggers__c);
                
    }
}